home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2001 May / SGI IRIX Base Documentation 2001 May.iso / usr / share / catman / p_man / cat3 / librw / RWBench.z / RWBench
Encoding:
Text File  |  1998-10-30  |  15.9 KB  |  529 lines

  1.  
  2.  
  3.  
  4. RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))                                                    RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))
  5.  
  6.  
  7.  
  8. NNNNaaaammmmeeee
  9.      RWBench - Rogue Wave library class
  10.  
  11. SSSSyyyynnnnooooppppssssiiiissss
  12.               #include <rw/bench.h>
  13.  
  14.  
  15.  
  16.               ((((AAAAbbbbssssttttrrrraaaacccctttt bbbbaaaasssseeee ccccllllaaaassssssss))))
  17.  
  18.  
  19.  
  20.  
  21. DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn
  22.      This is an abstract class that can automate the process of benchmarking a
  23.      piece of code.  To use it, derive a class from RRRRWWWWBBBBeeeennnncccchhhh, including a
  24.      definition for the virtual function ddddooooLLLLoooooooopppp((((uuuunnnnssssiiiiggggnnnneeeedddd lllloooonnnngggg NNNN))))....  This
  25.      function should perform NNNN operations of the type that you are trying to
  26.      benchmark.  RRRRWWWWBBBBeeeennnncccchhhh will call ddddooooLLLLoooooooopppp(((()))) over and over again until a preset
  27.      amount of time has elapsed.  It will then sum the total number of
  28.      operations performed.  To run, construct an instance of your derived
  29.      class and then call ggggoooo(((()))).  Then call rrrreeeeppppoooorrrrtttt(((()))) to get a standard summary.
  30.      For many compilers, this summary will automatically include the compiler
  31.      type and memory model.  You can call ooooppppssss(((()))),,,, oooouuuutttteeeerrrrLLLLooooooooppppssss(((()))),,,, eeeettttcccc. for more
  32.      detail.  If you wish to correct for overhead, then provide an iiiiddddlllleeeeLLLLoooooooopppp(((())))
  33.      function which should do all non-benchmark-related calculations.
  34.  
  35. PPPPeeeerrrrssssiiiisssstttteeeennnncccceeee
  36.      None
  37.  
  38. EEEExxxxaaaammmmpppplllleeee
  39.      This example benchmarks the time required to return a hash value for a
  40.      Rogue Wave string versus a Borland string.
  41.  
  42.               #include <rw/bench.h>                 /* Benchmark software */
  43.  
  44.  
  45.  
  46.               #include <rw/cstring.h>               /* Rogue Wave string class */
  47.           #include <stdlib.h>
  48.           #include <iostream.h>
  49.           #include <rw/ctoken.h>
  50.           #include <rw/regexp.h>
  51.           // The string to be hashed:
  52.           const char* cs = "A multi-character string with lots of words in it to be parsed out and searched for.";
  53.           class TestBrute : public RWBench {
  54.           public:
  55.           TestBrute() { }
  56.             virtual void       doLoop(unsigned long n);
  57.             virtual void       idleLoop(unsigned long n);
  58.             virtual void       what(ostream& s) const
  59.               { s << "Brute force string search: 0; }
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))                                                    RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))
  71.  
  72.  
  73.  
  74.           };
  75.           class TestRW : public RWBench {
  76.           public:
  77.           TestRW() { }
  78.             virtual void     doLoop(unsigned long n);
  79.             virtual void       idleLoop(unsigned long n);
  80.             virtual void       what(ostream& s) const
  81.               { s << "Rogue Wave search: 0; }
  82.           };
  83.           main(int argc, char* argv[]){
  84.             cout << "Testing string
  85.             // Test brute force string search algorithm:
  86.             TestBrute other;
  87.             other.parse(argc, argv);
  88.             other.go();
  89.             other.report(cout);
  90.             // Test RW searching w/regular expressions:
  91.             TestRW rw;
  92.             rw.parse(argc, argv);
  93.             rw.go();
  94.             rw.report(cout);
  95.             return 0;
  96.           }
  97.           void TestBrute::doLoop(unsigned long n){
  98.             RWCString string(cs);
  99.             RWCTokenizer *tokener;
  100.             RWCString token;
  101.             tokener = new RWCTokenizer(string);
  102.             while(n--){
  103.  
  104.               if((token = (*tokener)()).isNull())
  105.               {
  106.                   delete tokener;
  107.                   tokener = new RWCTokenizer(string);
  108.                   token = (*tokener)();
  109.               }
  110.               size_t j = 0;
  111.               for(size_t i = 0; i < string.length() && j != token.length();
  112.                   i++)
  113.               {
  114.                  j = 0;
  115.                  while((j < token.length()) && (string[i+j]==token[j]))
  116.                     j++;
  117.               }
  118.             }
  119.            delete tokener;
  120.           }
  121.           void TestRW::doLoop(unsigned long n){
  122.             RWCString string(cs);
  123.             RWCTokenizer *tokener;
  124.             RWCString token, result;
  125.             RWCRegexp re("");
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))                                                    RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))
  137.  
  138.  
  139.  
  140.             tokener = new RWCTokenizer(string);
  141.  
  142.  
  143.  
  144.  
  145.                 while(n--){
  146.  
  147.  
  148.  
  149.  
  150.              if((token = (*tokener)()).isNull())
  151.               {
  152.                   delete tokener;
  153.                   tokener = new RWCTokenizer(string);
  154.                   token = (*tokener)();
  155.               }
  156.  
  157.  
  158.  
  159.                 re = RWCRegexp(token);
  160.  
  161.  
  162.  
  163.                 result = string(re);       //Do the search!
  164.  
  165.  
  166.  
  167.                 }
  168.  
  169.  
  170.  
  171.                delete tokener;
  172.           }
  173.  
  174.  
  175.  
  176.               void TestBrute::idleLoop(unsigned long n){
  177.  
  178.  
  179.  
  180.                 RWCString string(cs);           // Subtract out the overhead
  181.             RWCTokenizer *tokener;
  182.             RWCString token;
  183.             tokener = new RWCTokenizer(string);
  184.  
  185.             while(n--){
  186.  
  187.              if((token = (*tokener)()).isNull())
  188.               {
  189.                   delete tokener;
  190.                   tokener = new RWCTokenizer(string);
  191.                   token = (*tokener)();
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))                                                    RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))
  203.  
  204.  
  205.  
  206.               }
  207.  
  208.  
  209.                 }
  210.  
  211.  
  212.  
  213.                delete tokener;
  214.           }
  215.           void TestRW::idleLoop(unsigned long n){
  216.             RWCString string(cs);                //Subtract out the overhead
  217.             RWCTokenizer *tokener;
  218.             RWCString token, result;
  219.             RWCRegexp re("");
  220.  
  221.  
  222.  
  223.                 tokener = new RWCTokenizer(string);
  224.  
  225.  
  226.  
  227.  
  228.             while(n--){
  229.  
  230.  
  231.  
  232.  
  233.              if((token = (*tokener)()).isNull())
  234.               {
  235.                   delete tokener;
  236.                   tokener = new RWCTokenizer(string);
  237.                   token = (*tokener)();
  238.               }
  239.  
  240.  
  241.                 re = RWCRegexp(token);
  242.             }
  243.  
  244.  
  245.  
  246.                delete tokener;
  247.           }
  248.  
  249.      PPPPrrrrooooggggrrrraaaammmm oooouuuuttttppppuuuutttt::::
  250.  
  251.               Testing string
  252.  
  253.  
  254.  
  255.               "A multi-character string with lots of words in it to be parsed out and searched for."
  256.           Borland C++ V4.0
  257.           Brute force string search:
  258.  
  259.  
  260.  
  261.                                                                         PPPPaaaaggggeeee 4444
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))                                                    RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))
  269.  
  270.  
  271.  
  272.           Iterations:                 35
  273.           Inner loop operations:      1000
  274.           Total operations:           35000
  275.           Elapsed (user) time:        4.596
  276.           Kilo-operations per second: 7.61532
  277.           Borland C++ V4.0
  278.           Rogue Wave search:
  279.           Iterations:                 53
  280.           Inner loop operations:      1000
  281.           Total operations:           53000
  282.           Elapsed (user) time:        2.824
  283.  
  284. PPPPuuuubbbblllliiiicccc CCCCoooonnnnssssttttrrrruuuuccccttttoooorrrrssss
  285.      Kilo-operations per second: 18.7677
  286.  
  287.  
  288.  
  289.               RWBench(double duration = 5, unsigned long ILO=1000,
  290.                   const char* machine = 0);
  291.  
  292.  
  293.      The parameter dddduuuurrrraaaattttiiiioooonnnn is the nominal amount of time that the benchmark
  294.      should take in seconds.  The virtual function ddddooooLLLLoooooooopppp((((uuuunnnnssssiiiiggggnnnneeeedddd lllloooonnnngggg)))) will
  295.      be called over and over again until at least this amount of time has
  296.      elapsed.  The parameter IIIILLLLOOOO is the number of "inner loop operations" that
  297.      should be performed.  This parameter will be passed in as parameter NNNN to
  298.      ddddooooLLLLoooooooopppp((((NNNN)))).  Parameter mmmmaaaacccchhhhiiiinnnneeee is an optional null terminated string that
  299.      should describe the test environment (perhaps the hardware the benchmark
  300.      is being run on ).
  301.  
  302. PPPPuuuubbbblllliiiicccc MMMMeeeemmmmbbbbeeeerrrr FFFFuuuunnnnccccttttiiiioooonnnnssss
  303.               virtual void
  304.           ddddooooLLLLoooooooopppp(unsigned long N)=0;
  305.  
  306.  
  307.      A pure virtual function whose actual definition should be supplied by the
  308.      specializing class.  This function will be repeatedly called until a time
  309.      duration has elapsed.  It should perform the operation to be benchmarked
  310.      NNNN times.  See the example.
  311.  
  312.               double
  313.           dddduuuurrrraaaattttiiiioooonnnn() const;
  314.  
  315.  
  316.      Return the current setting for the benchmark test duration.  This should
  317.      not be confused with function ttttiiiimmmmeeee(((()))) which returns the actual test time.
  318.  
  319.               virtual void
  320.           ggggoooo();
  321.  
  322.  
  323.      Call this function to run the benchmark.
  324.  
  325.  
  326.  
  327.                                                                         PPPPaaaaggggeeee 5555
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))                                                    RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))
  335.  
  336.  
  337.  
  338.               virtual void
  339.           iiiiddddlllleeeeLLLLoooooooopppp(unsigned long N);
  340.  
  341.  
  342.      This function can help to correct the benchmark for overhead.  The
  343.      default definition merely executes a "ffffoooorrrr(((())))" loop NNNN times.  See the
  344.      example.
  345.  
  346.               const char *
  347.           mmmmaaaacccchhhhiiiinnnneeee();
  348.  
  349.  
  350.      This function accesses the name of the machine which is passed into the
  351.      benchmark object through ppppaaaarrrrsssseeee(((()))).
  352.  
  353.               virtual void
  354.           ppppaaaarrrrsssseeee(int argc, char* argv[]);
  355.  
  356.  
  357.      This function allows an easy way to change the test duration, number of
  358.      inner loops and machine description from the command line:
  359.  
  360.  
  361.      AAAArrrrgggguuuummmmeeeennnntttt TTTTyyyyppppeeee DDDDeeeessssccccrrrriiiippppttttiiiioooonnnn
  362.  
  363.  
  364.      aaaarrrrggggvvvv[[[[1111]]]] ddddoooouuuubbbblllleeee Duration (sec.)
  365.  
  366.  
  367.      aaaarrrrggggvvvv[[[[2222]]]] uuuunnnnssssiiiiggggnnnneeeedddd lllloooonnnngggg No. of inner loops
  368.  
  369.  
  370.      aaaarrrrggggvvvv[[[[3333]]]] ccccoooonnnnsssstttt cccchhhhaaaarrrr**** Machine
  371.  
  372.  
  373.           void
  374.           ppppaaaarrrrsssseeee(const char *);
  375.  
  376.  
  377.      This is a non-virtual function which provides the same service as
  378.      ppppaaaarrrrsssseeee((((iiiinnnntttt aaaarrrrggggcccc,,,, cccchhhhaaaarrrr **** aaaarrrrggggvvvv[[[[]]]])))),  but is designed for Windows users.  It
  379.      extracts tokens from the null-terminated command argument provided by
  380.      Windows, then calls the virtual ppppaaaarrrrsssseeee for ANSI C command arguments.
  381.  
  382.               virtual void
  383.           rrrreeeeppppoooorrrrtttt(ostream&) const;
  384.  
  385.  
  386.      Calling this function provides an easy and convenient way of getting an
  387.      overall summary of the results of a benchmark.
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                                                         PPPPaaaaggggeeee 6666
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))                                                    RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))
  401.  
  402.  
  403.  
  404.               double
  405.           sssseeeettttDDDDuuuurrrraaaattttiiiioooonnnn(double t);
  406.  
  407.  
  408.      Change the test duration to time tttt.
  409.  
  410.               unsigned long
  411.           sssseeeettttIIIInnnnnnnneeeerrrrLLLLooooooooppppssss(unsigned long N);
  412.  
  413.  
  414.      Change the number of "inner loop operations" to NNNN.
  415.  
  416.               virtual void
  417.           wwwwhhhhaaaatttt(ostream&) const;
  418.  
  419.  
  420.      You can supply a specializing version of this virtual function that
  421.      provides some detail of what is being benchmarked.  It is called by
  422.      rrrreeeeppppoooorrrrtttt(((()))) when generating a standard report.
  423.  
  424.               void
  425.           wwwwhhhheeeerrrreeee(ostream&) const;
  426.  
  427.  
  428.      This function will print information to the stream about the compiler and
  429.      memory model that the code was compiled under.
  430.  
  431.               unsigned long
  432.           iiiinnnnnnnneeeerrrrLLLLooooooooppppssss() const;
  433.  
  434.  
  435.      Returns the current setting for the number of inner loop operations that
  436.      will be passed into function ddddooooLLLLoooooooopppp((((uuuunnnnssssiiiiggggnnnneeeedddd lllloooonnnngggg NNNN)))) as parameter NNNN.
  437.  
  438.               double
  439.           ttttiiiimmmmeeee() const;
  440.  
  441.  
  442.      Returns the amount of time the benchmark took, corrected for overhead.
  443.  
  444.               unsigned long
  445.           oooouuuutttteeeerrrrLLLLooooooooppppssss() const;
  446.  
  447.  
  448.      Returns the number of times the function ddddooooLLLLoooooooopppp(((()))) was called.
  449.  
  450.               double
  451.           ooooppppssss() const;
  452.  
  453.  
  454.      Returns the total number of inner loop operations that were performed
  455.      (the product of the number of times oooouuuutttteeeerrrrLLLLoooooooopppp(((()))) was called times the
  456.  
  457.  
  458.  
  459.                                                                         PPPPaaaaggggeeee 7777
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))                                                    RRRRWWWWBBBBeeeennnncccchhhh((((3333CCCC++++++++))))
  467.  
  468.  
  469.  
  470.      number of inner loop operations performed per call).
  471.  
  472.               double
  473.           ooooppppssssRRRRaaaatttteeee() const;
  474.  
  475.  
  476.      Returns the number of inner loop operations per second.
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                                         PPPPaaaaggggeeee 8888
  526.  
  527.  
  528.  
  529.